package unq.tpi.persistencia.alumnos.dao.hbm;

import org.hibernate.Query;

import unq.tpi.persistencia.alumnos.arq.runner.HibernateRunner;
import unq.tpi.persistencia.alumnos.dao.MateriaDAO;
import unq.tpi.persistencia.alumnos.model.Materia;

public class MateriaDAOHbm implements MateriaDAO {

	public Materia getPorCodigo(String codigoMateria) {
		String hql = "from Materia as m " +
						"where m.codigo = :codigo";
		Query query = HibernateRunner.currentSession().createQuery(hql);
		query.setParameter("codigo", codigoMateria);
		
		Materia result = (Materia) query.uniqueResult();
		return result; 
	}

	public void save(Materia materia) {
		HibernateRunner.currentSession().save(materia);
	}
	
	public void delete(Materia materia){
		HibernateRunner.currentSession().delete(materia);
	}

	@Override
	public Integer getCantidadAprobados(String codigoMateria) {
		String hql = "select count(*) from Cursada as c " +
						"inner join c.materia as m " +
						"where m.codigo = :codigo and c.nota >= 7";
		Query query = HibernateRunner.currentSession().createQuery(hql);
		query.setParameter("codigo", codigoMateria);
			
		Integer result = (Integer) query.uniqueResult();
		
		return result;
	}
}
